<?php
require_once "config.php";
require_once "filter.php";
require_once "simplepie/simplepie.inc";
require_once "Connection.php";
require_once "GoogleAPI.php";
session_start();

/*
 * first: lookup the actual feed url
 */
$google = new GoogleAPI();
$response = $google->lookupFeed($_POST['QUERY_URL']);

//print_r($response);exit; 

if($response->responseStatus != 200){
	echo json_encode(array('update_status'=>false,'msg'=>'新增失敗'));
	exit;
}
else{
	$url = $response->responseData->url;
}

error_log($url);
/*
 * second: find the feed provider's title
 */
if(isset($_SESSION['LOGIN_INFO']['id']) && isset($url)){
	
	$feed = new SimplePie();
	$feed->set_feed_url($url);
	$feed->force_feed(FALSE);
	$feed->init();
	$feed->handle_content_type();
	$title = $feed->get_title();
}
else{
	echo json_encode(array('update_status'=>false,'msg'=>'新增失敗'));
	exit;
}

$conn = new Connection();
$data = $conn->addSubscription($url,$title);

echo json_encode($data);

?>